<?php
/**
 * 前台用户控制器
 */
class UserController extends PlatformController
{
	/**
	 * 注册功能
	 */
	public function registerAction()
	{
		$master=Factory::M('MasterModel');
		$masterInfo=$master->getMasterInfo();
		$this->assign('masterInfo',$masterInfo);
		$article=Factory::M('ArticleModel');
		$newArt=$article->getNewArt(8);
		$this->assign('newArt',$newArt);
		$hotArtByHits=$article->getHotArtByHits(8);
		$this->assign('hotArtByHits',$hotArtByHits);
		$this->display('register.html');
	}	
	public function dealRegisterAction()
	{
		$userInfo=array();
		$user_name=$this->escapeData($_POST['user_name']);
		if(empty($user_name))
		{
			$this->jump("index.php?p=Home&c=User&a=register",':(用户名不能为空');
		}
		$user=Factory::M('UserModel');
		if($user->ifNameExists($user_name))
		{
			$this->jump("index.php?p=Home&c=User&a=register",':(用户名已存在');
		}
		
		$userInfo['user_name']=$user_name;
		$user_pass1=trim($_POST['pass1']);
		$user_pass2=trim($_POST['pass2']);
		if(empty($user_pass1) || empty($user_pass2))
		{
			$this->jump("index.php?p=Home&c=User&a=register",':(密码不能为空');
		}
		if($user_pass1!=$user_pass2)
		{
			$this->jump("index.php?p=Home&c=User&a=register",':(两次密码不相同');
		}
		$userInfo['user_pass']=md5($user_pass1);
		// var_dump($_FILES['user_image']);die;
		 // var_dump(isset($_FILES['user_image']));die;
		// var_dump($_FILES['user_image']['error']);die;
		if($_FILES['user_image']['error']!=4)
		{
			$upload=new Upload();
			$allow=array('image/png','image/gif','image/jpg','image/jpeg');
			$path=UPLOADS_DIR.'user';
			$result=$upload->uploadAction($_FILES['user_image'],$allow,$path);
			if($result)
			{
				$userInfo['user_image']=$result;
			}
			else
			{
				$this->jump("index.php?p=Home&c=User&a=register",Upload::$error);
			}
		}
		else
		{
			$this->jump("index.php?p=Home&c=User&a=register",'请上传头像');
		}
		if($user->insertUser($userInfo))
		{
			$this->jump("index.php?p=Home&c=User&a=login",'注册成功,登录吧');
		}
	}
	/**
	 * 会员登录
	 */
	public function loginAction()
	{
		$master=Factory::M('MasterModel');
		$masterInfo=$master->getMasterInfo();
		$this->assign('masterInfo',$masterInfo);
		$article=Factory::M('ArticleModel');
		$newArt=$article->getNewArt(8);
		$this->assign('newArt',$newArt);
		$hotArtByHits=$article->getHotArtByHits(8);
		$this->assign('hotArtByHits',$hotArtByHits);
		$this->display('login.html');
	}
	public function dealLoginAction()
	{
		$user_name=$this->escapeData($_POST['user_name']);
		$user_pass=trim($_POST['pass']);
		if(empty($user_name)||empty($user_pass))
		{
			$this->jump("index.php?p=Home&c=User&a=login",':(用户名或密码为空');
		}
		$user=Factory::M('UserModel');
		$result=$user->check($user_name,md5($user_pass));
		if($result)
		{
			@session_start();
			$_SESSION['userInfo']=$result;
			$this->jump("index.php?p=Home&c=Index&a=index");
		}
		else
		{
			$this->jump("index.php?p=Home&c=User&a=login",'用户名或密码错误');
		}
	}
	public function logoutAction()
	{
		unset($_SESSION['userInfo']);
		$this->jump("index.php?p=Home&c=Index&a=index");	
	}
}